package com.example.thornchg.yc_plane_game;

import android.view.MotionEvent;

import org.cocos2d.layers.CCLayer;
import org.cocos2d.layers.CCScene;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCLabel;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.transitions.CCFlipXTransition;
import org.cocos2d.types.CGPoint;
import org.cocos2d.types.CGRect;
import org.cocos2d.types.CGSize;

import java.util.ArrayList;

public class GameOverLayer extends CCLayer {
    private CGRect cgRect_rank;
    private CGRect cgRect;
    private CCLabel ccLabel_gameOver;

    public GameOverLayer(int gameScore) {
        CGSize winSize = CCDirector.sharedDirector().winSize();
        CombatMap combatMap=new CombatMap(this);

        MyDatabaseHelper myDatabaseHelper=new MyDatabaseHelper(CCDirector.theApp,"planeGame.db",
                null,1);
        DBHandle dbHandle=new DBHandle();
        ArrayList<String> historyScoreList = dbHandle.getHistoryScoreList(myDatabaseHelper.getReadableDatabase());
        dbHandle.insert(myDatabaseHelper.getWritableDatabase(),gameScore);



        CCLabel ccLabel1=CCLabel.makeLabel("本局得分","Arial",50);
        ccLabel1.setPosition(ccp(winSize.getWidth()/2,winSize.getHeight()/6*5));
        addChild(ccLabel1);
        CCLabel ccLabel2=CCLabel.makeLabel(gameScore+"","Arial",80);
        ccLabel2.setScale(2f);
        ccLabel2.setPosition(ccp(winSize.getWidth()/2,winSize.getHeight()/6*4));
        addChild(ccLabel2);
        CCLabel ccLabel3=CCLabel.makeLabel("历史最高得分："+historyScoreList.get(0),"Arial",50);
        ccLabel3.setPosition(ccp(winSize.getWidth()/2,winSize.getHeight()/6*3));
        addChild(ccLabel3);

        // 添加再来一局按钮
        CCSprite ccSprite_again_button = CCSprite.sprite("again_button.png");
        ccSprite_again_button.setScale(2);
        ccSprite_again_button.setPosition(winSize.getWidth() / 2, winSize.getHeight() / 3);
        addChild(ccSprite_again_button);

        cgRect=CGRect.make(
                ccSprite_again_button.getPosition().x-ccSprite_again_button.getBoundingBox().size.getWidth()/2,
                ccSprite_again_button.getPosition().y-ccSprite_again_button.getBoundingBox().size.getHeight()/2,
                ccSprite_again_button.getBoundingBox().size.getWidth(),
                ccSprite_again_button.getBoundingBox().size.getHeight()
        );
        CCSprite ccSprite_rank_button = CCSprite.sprite("Ranking.png");
        ccSprite_rank_button.setScale(2);
        ccSprite_rank_button.setPosition(winSize.getWidth() / 2, winSize.getHeight() / 5);
        addChild(ccSprite_rank_button);
        cgRect_rank = CGRect.make(
                ccSprite_rank_button.getPosition().x - ccSprite_rank_button.getBoundingBox().size.width / 2,
                ccSprite_rank_button.getPosition().y - ccSprite_rank_button.getBoundingBox().size.height / 2,
                ccSprite_rank_button.getBoundingBox().size.width,
                ccSprite_rank_button.getBoundingBox().size.height);

        setIsTouchEnabled(true);
    }

    @Override
    public boolean ccTouchesBegan(MotionEvent event) {
        CGPoint cgPoint = convertTouchToNodeSpace(event);
        if (CGRect.containsPoint(cgRect,cgPoint)){
            CCScene ccScene=CCScene.node();
            ccScene.addChild(new GameLayer());
            CCDirector.sharedDirector().runWithScene(ccScene);
        }else if (CGRect.containsPoint(cgRect_rank, cgPoint)) {
            CCScene ccScene=CCScene.node();
            ccScene.addChild(new RankLayer());
            CCFlipXTransition ccFlipXTransition=CCFlipXTransition.transition(1f,ccScene,1);
            CCDirector.sharedDirector().runWithScene(ccFlipXTransition);


        }

        return super.ccTouchesBegan(event);
    }
}
